package com.example.stock.business;

import com.example.stock.controller.vo.StockInfoVO;
import com.example.stock.excel.StockOperate;
import com.example.stock.model.DailyDetail;
import com.example.stock.model.MonthDetail;
import com.example.stock.model.WeekDetail;
import com.example.stock.service.*;
import com.example.stock.util.NumberUtil;
import com.example.stock.util.OperationConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class StockHoldBusiness {

    @Autowired
    StockOperationService stockOperationService;

    @Autowired
    StockService stockService;

    @Autowired
    WeekDataService weekDataService;

    @Autowired
    MonthDataService monthDataService;

    @Autowired
    YearDataService yearDataService;

    @Autowired
    DailyDataService dailyDataService;

    @Cacheable(value = "getHoldStocks")
    public List<Object> getHoldStocks() {
        List<Object> ret = new ArrayList<>();

        stockOperationService.operationDataGroupByStock().forEach((stock, operates) -> {
            Map<String, Object> item = new HashMap<>();

            int hold = operates.stream()
                    .filter(stockOperate -> Arrays.asList(new String[]{OperationConstant.BUY, OperationConstant.SELL}).contains(stockOperate.getOperation()))
                    .mapToInt(StockOperate::getOperateNum).sum();

            // 只添加持仓中的个股
            if (hold > 0) {
                StockInfoVO stockInfo = stockService.getStockInfo(stock);

                item.put("stock", stock);
                item.put("hold", hold);
                item.put("wugu", stockInfo.getWg());
                item.put("industry", stockInfo.getIndustry());
                item.put("category", stockInfo.getCategory());
                item.put("dividendYield", stockInfo.getDividendYield());

                // 市值、分位点
                WeekDetail currentWeekDetail = weekDataService.getCurrentWeekDetail(stock);
                if (currentWeekDetail != null) {
                    item.put("lp", currentWeekDetail.getLp());
                    item.put("holdMarketCap", NumberUtil.floor(currentWeekDetail.getClose() * 100 * hold,2)); // 持仓市值
                    item.put("wPct", currentWeekDetail.getWeekPct());
                    item.put("wIncNum", currentWeekDetail.getIncNum());
                }

                // month
                MonthDetail currentMonthDetail = monthDataService.getCurrentMonthDetail(stock);
                double price = currentMonthDetail.getClose();
                item.put("price", price);
                item.put("mPct", currentMonthDetail.getMonthPct());
                item.put("mIncNum", currentMonthDetail.getIncNum());

                // year
                item.put("yPct", yearDataService.getLastYearPct(stock));

                // 成交量 rank
                DailyDetail lastTradingRankByStock = dailyDataService.getLastedDailyDetailByStock(stock);
                item.put("dPct", lastTradingRankByStock.getPct());
                item.put("drawdown",  lastTradingRankByStock.getDrawdown());

                // 当前价格与最近一次购买价格的数据
                StockOperate lastBuy = operates.stream().filter(t -> t.getOperation().equals(OperationConstant.BUY)).toList().getLast();
                Double lastBuyPrice = lastBuy.getPrice();
                item.put("lastBuy", lastBuyPrice);
                item.put("lastBuyPct", NumberUtil.calPct(price,  lastBuyPrice));

                List<StockOperate> sells = operates.stream().filter(t -> t.getOperation().equals(OperationConstant.SELL)).toList();
                if (sells.isEmpty() ) {
                    item.put("lastSellPct", 0);
                } else {
                    Double lastSellPrice = sells.getLast().getPrice();
                    item.put("lastSellPct", NumberUtil.calPct(price , lastSellPrice));
                }

                ret.add(item);
            }
        });

        return ret;
    }
}
